package com.newtouch.xcd.admin.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 前台小姐配置
 */
@EnableSwagger2
@Configuration
public class CwrPointSwagger2Config {

    @Bean("pmsApis")
    public Docket pmsApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.1pms")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/pms.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                .globalOperationParameters(setHeaderToken())
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    @Bean("mtfApis")
    public Docket mtfApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.2mft")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/mft.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                .globalOperationParameters(setHeaderToken())
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    @Bean("mfpApis")
    public Docket mtpApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.3mfp")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/mfp.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                .globalOperationParameters(setHeaderToken())
                /* 设置安全模式，swagger可以设置访问token */
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    @Bean("maApis")
    public Docket maApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.4ma")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/ma.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                ;
    }

    @Bean("wxMenuApis")
    public Docket wxMenuApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.5wechatMenu")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/wx/menu.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                ;
    }

    @Bean("crmMenuApis")
    public Docket crmMenuApis() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2.6crm")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.regex("/crm.*"))
                .build()
                .apiInfo(apiInfo())
                .enable(true)
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("新致软件-新财道管理系统平台接口文档")
                .description("提供pms、wechat模块的文档")
                .termsOfServiceUrl("")
                .version("0.0.1")
                .build();
    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList = new ArrayList<ApiKey>();
        apiKeyList.add(new ApiKey("Authorization", "token", "header"));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    /**
     * 增加全局token
     *
     * @return
     */
    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }

}
